'use strict';

const Service = require('egg').Service;
const ConnectOption = require('../oltmodules/ConnectOption');
const ZTEOLT = require('../oltmodules/ZTEOLT');
const HWOLT = require('../oltmodules/HuaWeiOLT');
const ulid = require('../public/ulid');
class WANProfileService extends Service {
    async getWANLists(oltInformation, type, onuAddress) {
        const options = new ConnectOption(
            oltInformation.oltIP,
            oltInformation.snmpReadOnlyCommunity,
            oltInformation.snmpUDPPort,
            162,
            true,
            true,
            oltInformation.telnetTCPPort,
            oltInformation.oltManagementPassword,
            oltInformation.oltTelnetUsername,
            oltInformation.oltTelnetPassword,
            oltInformation.systemName,
            oltInformation.RZUsername,
            oltInformation.RZPassword,
            oltInformation.telnetProxySwitch
        );
        let methods = {
            "WANIP": async (oltInformation, me) => {
                let WANInfo;
                switch (oltInformation.oltType) {
                    case "ZTE":
                        const zte = new ZTEOLT(options);
                        WANInfo = await zte.getWANInfo(oltInformation.oltHardVersion, onuAddress, me)
                        break;

                    case "HUAWEI":
                        const hw = new HWOLT(options);
                        WANInfo = await hw.getWANInfo(oltInformation.oltHardVersion, onuAddress, me);
                        break;

                }
                return WANInfo
            },
            "VOIP": async (oltInformation, me) => {
                let VOIPInfo;
                switch (oltInformation.oltType) {
                    case "ZTE":
                        const zte = new ZTEOLT(options);
                        VOIPInfo = await zte.getVOIPInfo(oltInformation.oltHardVersion, onuAddress, me)
                        break;

                    case "HUAWEI":
                        const hw = new HWOLT(options);
                        VOIPInfo = await hw.getVOIPInfo(oltInformation.oltHardVersion, onuAddress, me);
                        break;

                }
                return VOIPInfo
            }
        }
        return await methods[type](oltInformation, this);
    }

    async createOrUpdateWan(oltInformation,params){
        const options = new ConnectOption(
            oltInformation.oltIP,
            oltInformation.snmpReadOnlyCommunity,
            oltInformation.snmpUDPPort,
            162,
            true,
            true,
            oltInformation.telnetTCPPort,
            oltInformation.oltManagementPassword,
            oltInformation.oltTelnetUsername,
            oltInformation.oltTelnetPassword,
            oltInformation.systemName,
            oltInformation.RZUsername,
            oltInformation.RZPassword,
            oltInformation.telnetProxySwitch
        );
        let result;
        switch (oltInformation.oltType) {
            case "ZTE":
                const zte = new ZTEOLT(options);
                result = await zte.createWan(oltInformation.oltHardVersion, params, this)
                break;

            case "HUAWEI":
                const hw = new HWOLT(options);
                result = await hw.createWan(oltInformation.oltHardVersion, params, this);
                break;

        }
        return result;
    }
    async deleteWan(oltInformation,params){
        const options = new ConnectOption(
            oltInformation.oltIP,
            oltInformation.snmpReadOnlyCommunity,
            oltInformation.snmpUDPPort,
            162,
            true,
            true,
            oltInformation.telnetTCPPort,
            oltInformation.oltManagementPassword,
            oltInformation.oltTelnetUsername,
            oltInformation.oltTelnetPassword,
            oltInformation.systemName,
            oltInformation.RZUsername,
            oltInformation.RZPassword,
            oltInformation.telnetProxySwitch
        );
        let result;
        switch (oltInformation.oltType) {
            case "ZTE":
                const zte = new ZTEOLT(options);
                result = await zte.deleteWan(oltInformation.oltHardVersion, params, this)
                break;

            case "HUAWEI":
                const hw = new HWOLT(options);
                result = await hw.deleteWan(oltInformation.oltHardVersion, params, this);
                break;

        }
        return result;
    }

    async deleteVoip(oltInformation,params){
        const options = new ConnectOption(
            oltInformation.oltIP,
            oltInformation.snmpReadOnlyCommunity,
            oltInformation.snmpUDPPort,
            162,
            true,
            true,
            oltInformation.telnetTCPPort,
            oltInformation.oltManagementPassword,
            oltInformation.oltTelnetUsername,
            oltInformation.oltTelnetPassword,
            oltInformation.systemName,
            oltInformation.RZUsername,
            oltInformation.RZPassword,
            oltInformation.telnetProxySwitch
        );
        let result;
        switch (oltInformation.oltType) {
            case "ZTE":
                const zte = new ZTEOLT(options);
                result = await zte.deleteVoip(oltInformation.oltHardVersion, params, this)
                break;

            case "HUAWEI":
                const hw = new HWOLT(options);
                result = await hw.deleteVoip(oltInformation.oltHardVersion, params, this);
                break;

        }
        return result;
    }

    async createOrUpdateVOIP(oltInformation,params){
        const options = new ConnectOption(
            oltInformation.oltIP,
            oltInformation.snmpReadOnlyCommunity,
            oltInformation.snmpUDPPort,
            162,
            true,
            true,
            oltInformation.telnetTCPPort,
            oltInformation.oltManagementPassword,
            oltInformation.oltTelnetUsername,
            oltInformation.oltTelnetPassword,
            oltInformation.systemName,
            oltInformation.RZUsername,
            oltInformation.RZPassword,
            oltInformation.telnetProxySwitch
        );
        let result;
        switch (oltInformation.oltType) {
            case "ZTE":
                const zte = new ZTEOLT(options);
                result = await zte.createOrUpdateVOIP(oltInformation.oltHardVersion, params, this)
                break;

            case "HUAWEI":
                const hw = new HWOLT(options);
                result = await hw.createOrUpdateVOIP(oltInformation.oltHardVersion, params, this);
                break;

        }
        return result;
    }
    async getCATV(oltInformation, onuAddress) {
        let result;
        const options = new ConnectOption(
            oltInformation.oltIP,
            oltInformation.snmpReadOnlyCommunity,
            oltInformation.snmpUDPPort,
            162,
            true,
            true,
            oltInformation.telnetTCPPort,
            oltInformation.oltManagementPassword,
            oltInformation.oltTelnetUsername,
            oltInformation.oltTelnetPassword,
            oltInformation.systemName,
            oltInformation.RZUsername,
            oltInformation.RZPassword,
            oltInformation.telnetProxySwitch
        );
        switch(oltInformation.oltType){
            case "ZTE":
                const zte = new ZTEOLT(options);
                result = await zte.getCATVState(oltInformation.oltHardVersion,onuAddress,this)
                break;
                
            case "HUAWEI":
                const hw = new HWOLT(options);
                result = await hw.getCATVState(oltInformation.oltHardVersion,onuAddress, this);
                break;

        }
        return result;
    }
    async updateEthState(oltInformation,params){
        let result;
        const options = new ConnectOption(
            oltInformation.oltIP,
            oltInformation.snmpReadOnlyCommunity,
            oltInformation.snmpUDPPort,
            162,
            true,
            true,
            oltInformation.telnetTCPPort,
            oltInformation.oltManagementPassword,
            oltInformation.oltTelnetUsername,
            oltInformation.oltTelnetPassword,
            oltInformation.systemName,
            oltInformation.RZUsername,
            oltInformation.RZPassword,
            oltInformation.telnetProxySwitch
        );
        switch(oltInformation.oltType){
            case "ZTE":
                const zte = new ZTEOLT(options);
                result = await zte.updateEthState(oltInformation.oltHardVersion,params,this)
                break;
                
            case "HUAWEI":
                const hw = new HWOLT(options);
                result = await hw.updateEthState(oltInformation.oltHardVersion,params, this);
                break;

        }
        return result;
    }
    async getEth(oltInformation, onuAddress) {
        let result;
        const options = new ConnectOption(
            oltInformation.oltIP,
            oltInformation.snmpReadOnlyCommunity,
            oltInformation.snmpUDPPort,
            162,
            true,
            true,
            oltInformation.telnetTCPPort,
            oltInformation.oltManagementPassword,
            oltInformation.oltTelnetUsername,
            oltInformation.oltTelnetPassword,
            oltInformation.systemName,
            oltInformation.RZUsername,
            oltInformation.RZPassword,
            oltInformation.telnetProxySwitch
        );
        switch(oltInformation.oltType){
            case "ZTE":
                const zte = new ZTEOLT(options);
                result = await zte.getETH(oltInformation.oltHardVersion,onuAddress,this)
                break;
                
            case "HUAWEI":
                const hw = new HWOLT(options);
                result = await hw.getETH(oltInformation.oltHardVersion,onuAddress, this);
                break;

        }
        return result;
    }
    /**
     * 刷新vlan-profile ip-profile sip-profile
     * @param {*} oltInfomation 
     * @returns 
     */
    async refreshOltProfile(oltInformation){
        let result;
        const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName, oltInformation.RZUsername, oltInformation.RZPassword, oltInformation.telnetProxySwitch);
        switch (oltInformation.oltType) {
            case "ZTE":
                const zte = new ZTEOLT(options);
                result = await zte.refreshOltProfile(oltInformation.oltHardVersion,  this)
                break;

            case "HUAWEI":
                // const hw = new HWOLT(options);
                // result = await hw.refreshOltProfile(oltInformation.oltHardVersion,  this);
                break;

        }
        return result
    }
    async insertOltProfiles(profiles,oltId,tid){
        //因为目前oltProfiles是用于创建WAN时传入profile name，所以与数据库中的id无关。
        //所以先删掉olt对应的profiles，在添加。
        profiles.forEach(element => {
            element._id = ulid.ulid();
            element.tid = tid;
            element.oltId = oltId;
        });
        await this.ctx.model.OltProfile.deleteMany( { oltId} );
        await this.ctx.model.OltProfile.create(profiles);
    }

    async createOltProfile(oltInformation,oltProfile){
        let result;
        const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName, oltInformation.RZUsername, oltInformation.RZPassword, oltInformation.telnetProxySwitch);
        switch(oltInformation.oltType){
            case "ZTE":
                const zte = new ZTEOLT(options)
                result = await zte.createOltprofile(oltInformation.oltHardVersion,oltProfile,this);
                break;
            case "HUAWEI":
                break;
        }
        return result;
    }
    async deleteOltProfile(oltInformation,oltProfile){
        let result;
        const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName, oltInformation.RZUsername, oltInformation.RZPassword, oltInformation.telnetProxySwitch);
        switch(oltInformation.oltType){
            case "ZTE":
                const zte = new ZTEOLT(options)
                result = await zte.deleteOltProfile(oltInformation.oltHardVersion,oltProfile,this);
                break;
            case "HUAWEI":
                break;
        }
        return result;
    }
}
module.exports = WANProfileService;